map, filter).Many modern array methods use a callback function that receives up to three arguments:
array.method((element, index, array) => {
// ... your logic here, operating on the element
});
element: The current element being processed in the array. (Required)index: The index of the current element. (Optional)array: The array the method was called upon. (Optional)Example with Arrow Function: const doubled = [1, 2, 3].map(num => num * 2);
| Method | Description | Returns |
|---|---|---|
forEach() | Executes a callback for each element in the array. | undefined |
map() | Creates a new array by calling a function on every element. | New array |
filter() | Creates a new array with all elements that pass a test (return true). | New array |
| Method | Description | Returns |
|---|---|---|
reduce() | Executes a reducer function to produce a single output value. | Single value |
every() | Tests if all elements in the array pass a test. | Boolean |
some() | Tests if at least one element in the array passes a test. | Boolean |
| Method | Description | Returns |
|---|---|---|
find() | Returns the first element that passes a test. | Element or undefined |
findIndex() | Returns the index of the first element that passes a test. | Index or -1 |
includes() | Determines whether an array includes a certain value. | Boolean |
| Method | Description | Type |
|---|---|---|
push() / pop() | Adds/removes an element from the end of an array. | Mutating |
unshift() / shift() | Adds/removes an element from the beginning of an array. | Mutating |
splice() | Adds/removes elements from an array at a specified index. | Mutating |
sort() | Sorts the elements of an array in place. | Mutating |
slice() | Returns a shallow copy of a portion of an array into a new array. | Non-Mutating |
concat() | Merges two or more arrays, returning a new array. | Non-Mutating |
forEach().map().filter().some().every().reduce().find().push(), pop(), splice(), etc.const result = users.filter(u => u.isActive).map(u => u.name);
const newArr = [...oldArr, newItem]; // Like push, but immutable
Understanding this distinction is crucial for avoiding bugs.
| Mutating Methods (Change Original Array) | Non-Mutating Methods (Return New Array) |
|---|---|
push(), pop(), shift(), unshift(), splice(), sort(), reverse(), fill() |
map(), filter(), slice(), concat(), reduce(), flat(), flatMap() |
Given an array of order objects, find the total revenue from all completed orders placed by customers in the USA.
const orders = [
{ id: 1, country: 'USA', status: 'completed', revenue: 100 },
{ id: 2, country: 'CAN', status: 'completed', revenue: 150 },
{ id: 3, country: 'USA', status: 'pending', revenue: 80 },
{ id: 4, country: 'USA', status: 'completed', revenue: 200 },
];
const totalUSARevenue = orders
.filter(order => order.country === 'USA' && order.status === 'completed') // 1. Get only completed US orders
.map(order => order.revenue) // 2. Get an array of just their revenue values
.reduce((sum, currentRevenue) => sum + currentRevenue, 0); // 3. Sum up the values
console.log(totalUSARevenue); // Output: 300
.sort() doesn't work for numbers.
sort() sorts elements as strings. You must provide a compare function: arr.sort((a, b) => a - b) for ascending order.forEach.
map or filter, or iterate backwards with a standard `for` loop.reduce on an empty array with no initial value.
TypeError. Always provide an initial value to reduce to handle empty arrays gracefully: arr.reduce(callback, initialValue).slice() vs. splice().
splice() modifies the original array, while slice() returns a new, shallow copy of a portion of it.